<?php

namespace Implementation;
use \Entity, \Core;

class Viewer
{
    protected 
    
    $table = array(),
    $result = array();
    
    function __clone() { }
    
    private function prepareFilesData($data)
    {
        $result = new Entity\Filepqp;
        $result = $result->prepareData($data);
        
        return str_replace('{{data}}', implode('', $result), '<table class="main" cellspacing="0">{{data}}</table></div>');
    }
    
    private function prepareLogConsoleData($data)
    {
        $result = new Entity\Consolepqp;
        $result = $result->prepareData($data);
        
        return implode('',(array) $result);
    }
    
    private function prepareLogMemoryData($data)
    {
        $result = new Entity\Memorypqp;
        $result = $result->prepareData($data);
        print_r($result);
        return implode('',(array) $result);
    }
    
    private function prepareLogSpeedData($data)
    {
        $result = new Entity\Speedpqp;
        $result = $result->prepareData($data);
        
        return implode('',(array) $result);
    }
    
    private function prepareLogSequenceData($data)
    {
        $result = new Entity\Sequencepqp;
        $result = $result->prepareData($data);
        
        return str_replace('{{data}}', implode('', $result), '<table class="main" cellspacing="0">{{data}}</table></div>');
    }
    
    private function prepareLogData($data)
    {
        
        foreach($data as $key => $val)
        {
            switch($key) 
            {
                case 'console':
                        $tamp = $this->prepareLogConsoleData($val['messages']);                
                        break;
                
                case 'memory':
                        $tamp = $this->prepareLogMemoryData($val['messages']);                
                        break;
                        
                case 'speed':
                        $tamp = $this->prepareLogSpeedData($val['messages']);                
                        break;
            }
            
            if(empty($tamp)) continue;
            $this->result [] = $tamp;
            unset($tamp);
        }
        
        return str_replace('{{data}}', implode('', $this->result), '<table class="main" cellspacing="0">{{data}}</table></div>');
    }
    
    private function prepareSummaryLog($data)
    {
        $table = ' <div id="pqp-console" class="pqp-box"><table class="side" cellspacing="0"> 
                        <tr> 
                        <td class="alt1"><var>{{totallogs}}</var><h4>Logs</h4></td> 
                        <td class="alt2"><var>{{totalerror}}</var> <h4>Errors</h4></td> 
                        </tr> 
                        <tr> 
                        <td class="alt3"><var>{{totalmemory}}</var> <h4>Memory</h4></td> 
                        <td class="alt4"><var>{{totalspeed}}</var> <h4>Speed</h4></td> 
                        </tr> 
                        </table>';
                        
        $table = str_replace('{{totallogs}}', $data['totallogs'], $table);
        $table = str_replace('{{totalerror}}', $data['totalerror'], $table);
        $table = str_replace('{{totalmemory}}', $data['totalmemory'], $table);
        $table = str_replace('{{totalspeed}}', $data['totalspeed'], $table);
        
        return $table;
    }
    
    private function prepareMemoryData($data)
    {
        return str_replace('{{data}}', $data, '<table class="main" cellspacing="0">{{data}}</table></div>');
    }
    
    private function prepareSummaryMemory($data)
    {
        $table = '<table class="side" cellspacing="0"> 
                        <tr><td><var>{{memoryused}}</var><h4>Used Memory</h4></td></tr> 
                        <tr><td class="alt"><var>{{memorytotal}}</var> <h4>Total Available</h4></td></tr> 
                        </table> ';
        
        $table = str_replace('{{memoryused}}', $data['memoryused'], $table);
        $table = str_replace('{{memorytotal}}', $data['memorytotal'], $table);
        
        return '<div id="pqp-memory" class="pqp-box">'.$table;                
                        
    }
    
    private function prepareSummarySpeed($data)
    {
        $table = '<div id="pqp-speed" class="pqp-box"><table class="side" cellspacing="0"> 
                        <tr><td><var>{{speedtotal}}</var><h4>Load Time</h4></td></tr> 
                        <tr><td class="alt"><var>{{speedallowed}}</var> <h4>Max Execution Time</h4></td></tr> 
                        </table> ';
                        
        $table = str_replace('{{speedtotal}}', $data['speedtotal'], $table);
        $table = str_replace('{{speedallowed}}', $data['speedallowed'], $table);
        
        return $table;                
    }
    
    private function prepareSpeedData($data)
    {
        return str_replace('{{data}}', $data, '<table class="main" cellspacing="0">{{data}}</table></div>');
    }
    
    private function prepareSummaryFiles($data)
    {
        $table = '<div id="pqp-files" class="pqp-box"><table class="side" cellspacing="0"> 
                        <tr><td><var>{{totalfiles}}</var><h4>Total Files</h4></td></tr> 
                        <tr><td class="alt"><var>{{totalsize}}</var> <h4>Total Size</h4></td></tr> 
                        <tr><td><var>{{largestsize}}</var> <h4>Largest</h4></td></tr> 
                        </table>';
        
        $table = str_replace('{{totalfiles}}', $data['totalfiles'], $table);
        $table = str_replace('{{totalsize}}', $data['totalsize'], $table);
        $table = str_replace('{{largestsize}}', $data['largestsize'], $table);
        
        return $table;
    }
    
    private function prepareQueriesData($data)
    {
        return '<div id="pqp-queries" class="pqp-box">
			<h3>This panel has no log items.</h3></div>';
    }
    
    private function prepareCss()
    {
        $host = Core\Request::instance()->serverhost();
        return '<link rel="stylesheet" type="text/css" href="'.$host.WEBFOLDER.'libs/quickprof/assets/css/pqp.css" media="screen" />';
    }
    
    private function prepareJavascript()
    {
        $host = Core\Request::instance()->serverhost();
        return '<script src="'.$host.WEBFOLDER.'libs/quickprof/assets/js/pqp.js"></script>';
    }
    
    private function prepareHeader($data)
    {
        $table = '<div id="pQp" class="console"> 
                        <table id="pqp-metrics" cellspacing="0"> 
                        <tr> <td class="green" onclick="changeTab(\'console\');"> 
                        <var>{{totallogs}}</var> 
                        <h4>Console</h4> 
                        </td> 
                        <td class="blue" onclick="changeTab(\'speed\');"> 
                        <var>{{totalspeed}}</var> 
                        <h4>Load Time</h4> 
                        </td> 
                        <td class="purple" onclick="changeTab(\'queries\');"> 
                        <var>{{totalqueries}}</var> 
                        <h4>Database</h4> 
                        </td> 
                        <td class="orange" onclick="changeTab(\'memory\');"> 
                        <var>{{totalmemory}}</var> 
                        <h4>Memory Used</h4> 
                        </td> 
                        <td class="red" onclick="changeTab(\'files\');"> 
                        <var>{{totalfiles}}</var> 
                        <h4>Included</h4> 
                        </td> 
                        </tr> 
                        </table>';
                        
        $table = str_replace('{{totallogs}}', $data['totallogs'], $table);
        $table = str_replace('{{totalqueries}}', $data['totalqueries'], $table);
        $table = str_replace('{{totalmemory}}', $data['totalmemory'], $table);
        $table = str_replace('{{totalspeed}}', $data['totalspeed'], $table);
        $table = str_replace('{{totalfiles}}', $data['totalfiles'], $table);

        return $table;
    }
    
    private function prepareFooter()
    {
        return '<table id="pqp-footer" cellspacing="0"> 
                        <tr> 
                        <td class="credit"> 
                        <a href="http://particletree.com" target="_blank"> 
                        <b class="green">Q</b><b class="blue">u</b><b class="purple">i</b><b class="orange">c</b><b class="red">k</b> 
                        Profiler 
                        </a>
                        </td> 
                        <td class="credit"><strong> &nbsp;&nbsp; Powered by: Chilik Framework &copy; 2011</strong></td>
                        <td class="actions"> 
                        
                        <a href="#" onclick="toggleDetails();return false">Details</a> 
                        <a class="heightToggle" href="#" onclick="toggleHeight();return false">Height</a> 
                        </td> 
                        </tr> 
                        </table>';
    }
    
    public function render($output)
    {
        
        $summarylog = array(
                        'totallogs' => $output['logs']['console']['count'],
                        'totalerror' => $output['logs']['errors']['count'],
                        'totalqueries' => $output['logs']['queries']['count'],
                        'totalmemory' => $output['memoryTotals']['memoryused'],
                        'totalspeed' => $output['speedTotals']['speedtotal'],
                        'totalfiles' => $output['fileTotals']['totalfiles'],
                    );
        
        $css = $this->prepareCss();
        $js = $this->prepareJavascript();
        $table = $this->prepareLogData($output['logs']);
        
        $this->table [] = $this->prepareHeader($summarylog);
        $this->table [] = $this->prepareSummaryLog($summarylog);
        $this->table [] = $this->prepareLogSequenceData($output['logs']['console']['sequence']);
        
        $this->table [] = $this->prepareSummarySpeed($output['speedTotals']);
        
        if(isset($this->result[2])) { $this->table [] = $this->prepareSpeedData($this->result[2]); }
        else { $this->table [] = '<div id="pqp-speed" class="pqp-box"><h3>This panel has no log items.</h3></div></div>'; }
        
        $this->table [] = $this->prepareQueriesData($output['logs']);
        
        $this->table [] = $this->prepareSummaryMemory($output['memoryTotals']);
        
        if(isset($this->result[1])) { $this->table [] = $this->prepareMemoryData($this->result[1]); }
        else { $this->table [] = '<div id="pqp-memory" class="pqp-box"><h3>This panel has no log items.</h3></div></div>'; }
        
        $this->table [] = $this->prepareSummaryFiles($output['fileTotals']);
        $this->table [] = $this->prepareFilesData($output['files']);
        
        $this->table [] = $this->prepareFooter();
        
        echo $css, $js,'<div id="pqp-container" class="pQp" style="display:none">', implode('',$this->table), '</div></div>';
        unset($css, $js, $summarylog);
    }
    
    function __destruct()
    {
        unset($this->result, $this->table, $this);
    }
}
